package com.atguigu.registration.hosp.controller.api;

import com.atguigu.registration.common.exception.RegistrationException;
import com.atguigu.registration.common.helper.HttpRequestHelper;
import com.atguigu.registration.common.result.Result;
import com.atguigu.registration.common.result.ResultCodeEnum;
import com.atguigu.registration.common.utils.MD5;
import com.atguigu.registration.hosp.service.DepartmentService;
import com.atguigu.registration.hosp.service.HospitalService;
import com.atguigu.registration.hosp.service.HospitalSetService;
import com.atguigu.registration.hosp.service.ScheduleService;
import com.atguigu.registration.model.hosp.Department;
import com.atguigu.registration.model.hosp.Hospital;
import com.atguigu.registration.model.hosp.Schedule;
import com.atguigu.registration.vo.hosp.DepartmentQueryVo;
import com.atguigu.registration.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: Jiang Yizhi
 * @Date: 2022/4/24 8:35 AM
 */
@RestController
@RequestMapping("api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        /** 获取医院系统传过来的签名，签名进行了MD5加密 */
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        String hospSign = (String)paramMap.get("sign");
        /** 获取医院编号 */
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        /** 把数据库查到的signKey进行MD5加密 */
        String signKeyMD5 = MD5.encrypt(signKey);

        /** 判断签名是否一致 */
        if (!hospSign.equals(signKeyMD5)) {
            throw new RegistrationException(ResultCodeEnum.SIGN_ERROR);
        }
        /** 调用service方法，根据医院编号进行查询 */
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation(value = "上传医院")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        /** 获取医院系统传过来的签名，签名进行了MD5加密 */
        String hospSign = (String)paramMap.get("sign");
        /** 根据传过来的hoscode查询数据库，查询签名 */
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        /** 把数据库查到的signKey进行MD5加密 */
        String signKeyMd5 = MD5.encrypt(signKey);

        /** 判断签名是否一致 */
        if (!hospSign.equals(signKeyMd5)) {
            throw new RegistrationException(ResultCodeEnum.SIGN_ERROR);
        }
        /** 传输中"+"变成了" "，需要改回来 */
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        hospitalService.save(paramMap);
        return Result.ok();
    }

    /** 上传科室接口 */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        verifySignKey(paramMap);
        departmentService.save(paramMap);
        return Result.ok();
    }

    /** 查询科室接口 */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        /** 获取传递过来科室信息 */
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        /** 医院编号 */
        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page"))? 1:Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))? 1:Integer.parseInt((String)paramMap.get("limit"));
        //TODO 签名校验
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        /** 调用service方法 */
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除科室接口
     * @param request
     * @return
     */
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        /** 医院编号 科室编号 */
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
//        verifySignKey(paramMap);
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }


    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //TODO 签名校验
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        //TODO 签名校验

        /** 当前页和每页记录数 */
        int page = StringUtils.isEmpty(paramMap.get("page"))? 1:Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))? 1:Integer.parseInt((String) paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        /** 调用service方法 */
        Page<Schedule> pageModel =  scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        /** 获取医院编号和排班编号 */
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        //TODO 签名校验
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    public void verifySignKey(Map<String, Object> paramMap) {
        /** 获取医院系统传过来的签名，签名进行了MD5加密 */
        String hospSign = (String)paramMap.get("sign");
        /** 根据传过来的hoscode查询数据库，查询签名 */
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        /** 把数据库查到的signKey进行MD5加密 */
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMd5)) {
            throw new RegistrationException(ResultCodeEnum.SIGN_ERROR);
        }
    }

}
